package com.strive.village.utils;

import com.alibaba.excel.EasyExcel;
import com.strive.village.entity.dto.ResidentDto;
import com.strive.village.entity.dto.VillageDto;
import com.strive.village.exception.EasyExcelException;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * 导出数据库文件到Excel工具类
 *
 * @description：
 * @author：StrivePeng
 */
public class DBExportUtils {

    /**
     * 导出村民信息工具类
     *
     * @param residentList 村民集合
     * @param filePath     文件路径
     */
    public static void exportResidentToExcel(List<ResidentDto> residentList, String filePath) {
        try (FileOutputStream outputStream = new FileOutputStream(new File(filePath))) {
            // 通过文件流写入系统
            EasyExcel.write(outputStream, ResidentDto.class).sheet("村民信息").doWrite(residentList);
        } catch (Exception e) {
            // 处理异常
            throw new EasyExcelException(e.getMessage(), e);
        }
    }

    /**
     * 导出乡村信息工具类
     *
     * @param villageDtoList 乡村信息
     * @param filePath       导出路径
     */
    public static void exportVillageToExcel(List<VillageDto> villageDtoList, String filePath) {
        try (FileOutputStream outputStream = new FileOutputStream(new File(filePath))) {
            EasyExcel.write(outputStream, VillageDto.class).sheet("乡村信息").doWrite(villageDtoList);
        } catch (Exception e) {
            throw new EasyExcelException(e.getMessage(), e);
        }
    }
}
